package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.reflection.ClassConvertUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.http.HttpContentType;
import com.ts.api.http.org.entity.apply.OrgApplyRongQuan;
import com.ts.api.http.org.entity.dataexchange.OrgDataExchangeRongQuan;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

/**
 *  南京融泉信息咨询有限公司
 *
 *  @author WJB
 */
@ApiPlan(productId = "48", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5RongQuan implements ApiOrgPhoneMd5ConvertHandler {

    private static final String BRAND = "1718";
    private static final String SOURCE = "rqjf-gat";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) {
        apiDataExchangeParameter.setHttpContentType(HttpContentType.APPLICATION_FORM_URLENCODED);

        OrgDataExchangeRongQuan data = new OrgDataExchangeRongQuan();
        data.setBrand(BRAND);
        data.setMd5mobile(apiDataExchangeParameter.getPhoneMd5());
        return data;
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }

        boolean code = Eq.object(jsonObject.getInteger("data"), 1);
        if (!code) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) {
        apiApplyParameter.setHttpContentType(HttpContentType.APPLICATION_FORM_URLENCODED);

        OrgApplyRongQuan data = new OrgApplyRongQuan();
        data.setName(apiApplyParameter.getUserName());
        data.setMobile(apiApplyParameter.getPhone());
        data.setSource(SOURCE);

        //  不能包含市
        String city = apiApplyParameter.getCity();
        if (ValidParam.isNotNull(city)) {
            city = city.replace("市", "");
            data.setCity(city);
        }

        //  车
        if (ValidParam.isNotEmpty(apiApplyParameter.getCarInfo())) {
            String car = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", "无")
            .end("有");
            data.setCar(car);
        }

        //  年龄
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        data.setAge(apiFieldConverter.getAgeInt());


        //  房产情况
        if (ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
            String house = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", "无")
                .end("有");
            data.setHouse(house);
        }

        //  保单
        if (ValidParam.isNotEmpty(apiApplyParameter.getInsurancePolicy())) {
            String baodanIs = ChooseEq.create()
                    .when(apiApplyParameter.getInsurancePolicy(), "1", "无")
                    .end("有");
            data.setBaodanIs(baodanIs);
        }

        //  性别
        String sex = apiApplyParameter.getSex();
        if (ValidParam.isNotEmpty(sex)) {
            String sexChoose = ChooseEq.create()
                .when(sex, "0", "男")
                .when(sex, "1", "女")
                .end("");
            data.setSex(sexChoose);
        }

        //  贷款额度
        Integer money = ChooseEq.create()
                .when(apiApplyParameter.getLoanAmount(), "1", 3)
                .when(apiApplyParameter.getLoanAmount(), "2", 5)
                .when(apiApplyParameter.getLoanAmount(), "3", 10)
                .when(apiApplyParameter.getLoanAmount(), "4", 20)
                .end();
        data.setMoney(money);

        //  社保
        if (ValidParam.isNotEmpty(apiApplyParameter.getSocialSecurity())) {
            String shebao = ChooseEq.create()
                    .when(apiApplyParameter.getSocialSecurity(), "1", "无")
                    .end("有");
            data.setShebao(shebao);
        }

        //  公积金
        if (ValidParam.isNotEmpty(apiApplyParameter.getProvidentFund())) {
            String gongjijin = ChooseEq.create()
                    .when(apiApplyParameter.getProvidentFund(), "1", "无")
                    .end("有");
            data.setGongjijin(gongjijin);
        }


        //  信用卡
        if (ValidParam.isNotEmpty(apiApplyParameter.getCreditLimit())) {
            String creditCard = ChooseEq.create()
                    .when(apiApplyParameter.getCreditLimit(), "1", "无")
                    .end("有");
            data.setCreditCard(creditCard);
        }

        //  企业
        String career = apiApplyParameter.getCareer();
        if (ValidParam.isEmpty(career)) {
            //  无数据为个人用户
            data.setCompany("无");
        } else {
            String choose = ChooseEq.create()
                    //  如果是企业就为企业用户
                    .when(career, "3", "有")
                    //  个人用户
                    .end("无");
            data.setCompany(choose);
        }

        //  银行代发
        if (ValidParam.isNotEmpty(apiApplyParameter.getSalaryPayMethod())) {
            String isbankpay = ChooseEq.create()
                    .when(apiApplyParameter.getSalaryPayMethod(), "1", "银行代发")
                    .end("无");
            data.setIsbankpay(isbankpay);
        }

        //  芝麻分
        if (ValidParam.isNotEmpty(apiApplyParameter.getSesameCredit())) {
            Integer zhima = ChooseEq.create()
                .when(apiApplyParameter.getSesameCredit(), "1", 600)
                .when(apiApplyParameter.getSesameCredit(), "2", 649)
                .when(apiApplyParameter.getSesameCredit(), "3", 699)
                .when(apiApplyParameter.getSesameCredit(), "4", 720)
            .end();
            data.setZhima(zhima);
        }

        //  时间
        String timestamp = ClassConvertUtils.toStr(System.currentTimeMillis());
        timestamp = timestamp.substring(0, timestamp.length() - 3);
        data.setTime(ClassConvertUtils.toInt(timestamp));

        return data;
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }

        boolean code = Eq.object(jsonObject.getInteger("code"), 200);
        if (!code) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
